package sz.szsmk.citizencard.ui.dialogs;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.StyleRes;

import sz.szsmk.citizencard.R;
import sz.szsmk.citizencard.util.Constant;
import sz.szsmk.citizencard.util.JumpUtils;

/**
 * author : hyt
 * date : 2020/2/13
 * description :
 */
public class ServiceAuthDialog extends Dialog {

    ImageView iv_close;

    Button btn_ok;

    CheckBox cb_authorization;

    TextView tv_auth;

    IServiceAuthDialog iServiceAuthDialog;

    public ServiceAuthDialog(@NonNull Context context) {
        this(context, R.style.dialog);

        int divierId = context.getResources().getIdentifier("android:id/titleDivider", null, null);
        View divider = findViewById(divierId);
        divider.setVisibility(View.GONE);
    }
    protected ServiceAuthDialog(@NonNull Context context, @StyleRes int themeResId) {
        super(context, R.style.alertdialog);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(LayoutInflater.from(getContext()).inflate(R.layout.dialog_citizenffinance, null));

        iv_close = findViewById(R.id.iv_close);

        btn_ok = findViewById(R.id.btn_ok);

        cb_authorization = findViewById(R.id.cb_authorization);

        tv_auth = findViewById(R.id.tv_auth);

        initView();
    }
    private void initView(){

        tv_auth.setText(Html.fromHtml("同意<font color='#2184FA'>《用户授权协议》</font>"));
        tv_auth.setOnClickListener(v->{

            String url = Constant.BASE_URL + "app-front-end/#/ng/agreement/userAuth";
            Bundle bundle = new Bundle();
            bundle.putString("url", url);
            JumpUtils.jumptoNatvie(getContext(), "webView", 0, bundle);

        });
        iv_close.setOnClickListener(v->
                ServiceAuthDialog.this.dismiss());
        cb_authorization.setOnCheckedChangeListener((buttonView, isChecked) -> btn_ok.setEnabled(isChecked));
        btn_ok.setOnClickListener(v->{
            if ( iServiceAuthDialog!=null){
                if(cb_authorization.isChecked()){
                    iServiceAuthDialog.onclick();
                    dismiss();
                }else{
                    Toast.makeText(getContext(),"请勾选协议",Toast.LENGTH_LONG).show();
                }
            }
        });

    }

    public IServiceAuthDialog getiServiceAuthDialog() {
        return iServiceAuthDialog;
    }

    public void setiServiceAuthDialog(IServiceAuthDialog iServiceAuthDialog) {
        this.iServiceAuthDialog = iServiceAuthDialog;
    }

    public interface IServiceAuthDialog{

        void onclick();
    }
}
